/* 
 * 11/19/04	 1.0 moved to LGPL.
 * 
 * 12/12/99  Initial Version based on FileObuffer.	mdm@techie.com.
 * 
 * FileObuffer:
 * 15/02/99  Java Conversion by E.B ,javalayer@javazoom.net
 *
 *-----------------------------------------------------------------------
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as published
 *   by the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------------
 */

package javazoom.jl.decoder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

/**
 * The <code>SampleBuffer</code> class implements an output buffer
 * that provides storage for a fixed size block of samples. 
 */
public class SampleBuffer extends Obuffer implements Externalizable
{
  private short[] 		buffer;
  private int[] 		bufferp;
  private int 			channels;
  private int			frequency;
  
  /*
   * for serialization only.  
   */
  public SampleBuffer() {

  }
  /**
   * Constructor
   */
  public SampleBuffer(int sample_frequency, int number_of_channels)
  {
  	buffer = new short[OBUFFERSIZE];
	bufferp = new int[MAXCHANNELS];
	channels = number_of_channels;
	frequency = sample_frequency;
	
	for (int i = 0; i < number_of_channels; ++i) 
		bufferp[i] = (short)i;
	
  }

  public int getChannelCount()
  {
	return this.channels;  
  }
  
  public int getSampleFrequency()
  {
	  return this.frequency;
  }
  
  public short[] getBuffer()
  {
	return this.buffer;  
  }
  
  public int getBufferLength()
  {
	  return bufferp[0];
  }
  
  /**
   * Takes a 16 Bit PCM sample.
   */
  public void append(int channel, short value)
  {
	buffer[bufferp[channel]] = value;
	bufferp[channel] += channels;	  	
  }
  
	public void appendSamples(int channel, float[] f)
	{
	    int pos = bufferp[channel];
		
		short s;
		float fs;
	    for (int i=0; i<32;)
	    {
		  	fs = f[i++];
			fs = (fs>32767.0f ? 32767.0f 
						   : (fs < -32767.0f ? -32767.0f : fs));
			
			s = (short)fs;
			buffer[pos] = s;
			pos += channels;
	    }
		
		bufferp[channel] = pos;
	}
  
  
  /**
   * Write the samples to the file (Random Acces).
   */
  public void write_buffer(int val)
  {
				  
	//for (int i = 0; i < channels; ++i) 
	//	bufferp[i] = (short)i;

  }

  public void close()
  {}
  
  /**
   *
   */
  public void clear_buffer()
  {
	for (int i = 0; i < channels; ++i) 
		bufferp[i] = (short)i;
  }

  /**
   *
   */
  public void set_stop_flag()
  {}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		channels = in.readInt();
		frequency = in.readInt();
	 	buffer = new short[OBUFFERSIZE];
		bufferp = new int[MAXCHANNELS];
		
		for(int i=0;i<buffer.length;i++) { buffer[i]=in.readShort(); };
		for(int i=0;i<bufferp.length;i++) { bufferp[i]=in.readInt(); };		
	}
	
	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeInt(channels);
		out.writeInt(frequency);
		for(short s:buffer) { out.writeShort(s); }
		for(int i:bufferp) {out.writeInt(i); }
	}
	
	public byte[] toBytes() throws IOException {
		ByteArrayOutputStream objectByteStream=new ByteArrayOutputStream();
		ObjectOutputStream objectStream=new ObjectOutputStream(objectByteStream);
		objectStream.writeObject(this);
		byte[] bytes=objectByteStream.toByteArray();
		objectStream.close();
		return bytes;
	}

	public static SampleBuffer fromBytes(byte[] bytes)  throws IOException {
		ByteArrayInputStream bytesStream=new ByteArrayInputStream(bytes);
		ObjectInputStream objectStream=new ObjectInputStream(bytesStream);
		SampleBuffer buffer;
		try {
			buffer = (SampleBuffer)objectStream.readObject();
		} catch (ClassNotFoundException e) { //shouldn't happen, since this is the class to instantiate.
			throw new RuntimeException(e);
		}
		objectStream.close();
		return buffer;
	}
}
